package com.warm.androidstudy.utils;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Locale;

/**
 * Created by Administrator on 2017/8/25.
 */

public class ObjectUtils {
    public ObjectUtils() {
    }

    public static String stringToAscii(String value) {
        if (TextUtils.isEmpty(value)) {
            return "";
        } else {
            StringBuffer sbu = new StringBuffer();
            char[] chars = value.toCharArray();

            for (int i = 0; i < chars.length; ++i) {
                sbu.append(chars[i]);
            }

            return sbu.toString();
        }
    }

    public static String asciiToString(String value) {
        if (TextUtils.isEmpty(value)) {
            return "";
        } else {
            StringBuffer sbu = new StringBuffer();
            String[] chars = value.split(",");

            for (int i = 0; i < chars.length; ++i) {
                sbu.append((char) Integer.parseInt(chars[i]));
            }

            return sbu.toString();
        }
    }

    public static ArrayList<?> removeDuplicateWithOrder(ArrayList<?> list) {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        ArrayList newList = new ArrayList();
        Iterator iter = list.iterator();

        while (iter.hasNext()) {
            Object element = iter.next();
            if (linkedHashSet.add(element)) {
                newList.add(element);
            }
        }

        return newList;
    }

    public static LinkedList<?> removeDuplicateWithOrder(LinkedList<?> list) {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        LinkedList newList = new LinkedList();
        Iterator iter = list.iterator();

        while (iter.hasNext()) {
            Object element = iter.next();
            if (linkedHashSet.add(element)) {
                newList.add(element);
            }
        }

        return newList;
    }

    public static String getFileEndName(String Strfile) {
        if (Strfile == null) {
            return "";
        } else {
            String end = Strfile.substring(Strfile.lastIndexOf(".") + 1, Strfile.length()).toLowerCase(Locale.ENGLISH);
            return end;
        }
    }

    public static String getFileEndName(File file) {
        String end = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase(Locale.ENGLISH);
        return end;
    }

    public static String getDivision(String Strfile, String st) {
        if (Strfile == null) {
            return "";
        } else {
            String end = Strfile.substring(Strfile.lastIndexOf(st) + 1, Strfile.length()).toLowerCase(Locale.ENGLISH);
            return end;
        }
    }

    public static String getFrontDivision(String Strfile, String st) {
        if (Strfile == null) {
            return "";
        } else {
            String Front = Strfile.substring(0, Strfile.lastIndexOf(st)).toLowerCase(Locale.ENGLISH);
            return Front;
        }
    }

    public static String getLastwords(String srcText, String p) {
        try {
            String[] e = TextUtils.split(srcText, p);
            int index = e.length - 1 < 0 ? 0 : e.length - 1;
            return e[index];
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String calculateVoiceTime(String file) {
        File _file = new File(file);
        if (!_file.exists()) {
            return "0";
        } else {
            try {
                long duration = getAmrDuration(_file);
                duration /= 1000L;
                return duration > 60L ? "60" : (duration < 1L ? "1" : String.valueOf(duration));
            } catch (IOException var5) {
                return "0";
            }
        }
    }

    public static long getAmrDuration(File file) throws IOException {
        long duration = -1L;
        int[] packedSize = new int[]{12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
        RandomAccessFile randomAccessFile = null;

        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            long length = file.length();
            int pos = 6;
            int frameCount = 0;
            boolean packedPos = true;
            byte[] datas = new byte[1];

            while (true) {
                if ((long) pos <= length) {
                    randomAccessFile.seek((long) pos);
                    if (randomAccessFile.read(datas, 0, 1) == 1) {
                        int var14 = datas[0] >> 3 & 15;
                        pos += packedSize[var14] + 1;
                        ++frameCount;
                        continue;
                    }

                    duration = length > 0L ? (length - 6L) / 650L : 0L;
                }

                duration += (long) (frameCount * 20);
                return duration;
            }
        } finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }

        }
    }

    public static String JudgeFileType(String Strfile) {
        if (Strfile == null) {
            return "";
        } else {
            String end = getFileEndName(Strfile);
            String fileName = Strfile;
            if (!end.equals("m4a") && !end.equals("mp3") && !end.equals("mid") && !end.equals("xmf") && !end.equals("ogg") && !end.equals("wav") && !end.equals("amr")) {
                if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) {
                    fileName = "[图片]";
                }
            } else {
                fileName = "[语音]";
            }

            return fileName;
        }
    }

    public static String isStrempty(String str) {
        return TextUtils.isEmpty(str) ? "" : (!str.equals("null") && !str.equals("nul") ? str : "");
    }

    public static boolean FileExisted(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    public boolean deleteFile(String filePath) {
        File file = new File(filePath);
        return file.exists() && file.isFile() ? file.delete() : false;
    }

    public boolean deleteDirectory(String filePath) {
        boolean flag = false;
        if (!filePath.endsWith(File.separator)) {
            filePath = filePath + File.separator;
        }

        File dirFile = new File(filePath);
        if (dirFile.exists() && dirFile.isDirectory()) {
            flag = true;
            File[] files = dirFile.listFiles();

            for (int i = 0; i < files.length; ++i) {
                if (files[i].isFile()) {
                    flag = this.deleteFile(files[i].getAbsolutePath());
                    if (!flag) {
                        break;
                    }
                } else {
                    flag = this.deleteDirectory(files[i].getAbsolutePath());
                    if (!flag) {
                        break;
                    }
                }
            }

            return !flag ? false : dirFile.delete();
        } else {
            return false;
        }
    }

    public boolean DeleteFolder(String filePath) {
        File file = new File(filePath);
        return !file.exists() ? false : (file.isFile() ? this.deleteFile(filePath) : this.deleteDirectory(filePath));
    }
}
